<template>
  <div class="login">
    <div class="container">
      <div class="header">
        <a href="/"><img src="/imgs/login-logo.png" alt="" /></a>
      </div>
    </div>
    <div class="wrapper">
      <div class="container">
        <div class="login-form">
          <h3>
            <span class="checked">帐号登录</span><span class="sep-line">|</span
            ><span>扫码登录</span>
          </h3>
          <div class="input">
            <input
              type="text"
              placeholder="请输入帐号"
              v-model="username"
              name="username"
            />
          </div>
          <div class="input">
            <input
              type="password"
              placeholder="请输入密码"
              v-model="password"
              name="password"
              @keyup.enter="login"
            />
          </div>

          <div><button class="btn" @click="login">登录</button></div>

          <div class="tips">
            <div class="sms">手机短信登录/注册</div>
            <div class="reg" @click="register">
              立即注册<span>|</span>忘记密码？
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="footer-link">
        <a href="https://www.imooc.com/u/1343480" target="_blank"
          >河畔一角主页</a
        ><span>|</span>
        <a href="https://coding.imooc.com/class/113.html" target="_blank"
          >Vue全栈课程</a
        ><span>|</span>
        <a href="https://coding.imooc.com/class/236.html" target="_blank"
          >React全家桶课程</a
        ><span>|</span>
        <a href="https://coding.imooc.com/class/343.html" target="_blank"
          >微信支付专项课程（H5+小程序/云+Node+MongoDB）</a
        >
      </div>
      <p class="copyright">
        Copyright ©2019 mi.futurefe.com All Rights Reserved.
      </p>
    </div>
  </div>
</template>

<script>
import requests from "@/api/ajax";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",

  data() {
    return {
      username: "",
      password: "",
      userId: "",
    };
  },

  mounted() {},

  methods: {
    //
    //       try {
    //         const { username, password } = this
    //         username &&
    //           password &&
    //           (await this.$store.dispatch('login', { username, password }))
    // // this.$cookie.set("userId",)
    //      this.$router.push({path:"home"})
    //      } catch (error) {
    //         alert(error)
    //       }

    login() {
      const { username, password } = this;
      username &&
        password &&
        requests
          .post("/user/login", {
            username,
            password,
          })
          .then((res) => {
            console.log(res);
            this.$cookie.set("userId", res.data.id, { expires: "Session" });
            this.$store.dispatch("saveUserName", res.data.username);

            this.$router.push("/home");
            this.$message.success("登陆成功");
         
          }).catch((res)=>{
            console.log(res);
            this.$message.error(res.msg);
          }
          );
    },
    register() {
      requests
        .post("/user/register", {
          username: "admin1",
          password: "admin1",
          email: "admin1@163.com",
        })
        .then(() => {
          this.$message.success("注册成功");
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  .container {
    height: 113px;
    img {
      width: auto;
      height: 100%;
    }
  }
  .wrapper {
    height: 576px;
    background: url("../../../public/imgs/login-bg.jpg") no-repeat center;
  }
  .login-form {
    box-sizing: border-box;
    padding-left: 31px;
    padding-right: 31px;
    width: 410px;
    height: 510px;
    background-color: #fffafa;
    position: absolute;
    z-index: 10;
    top: 20px;
    right: 0px;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    h3 {
      height: 23px;
      line-height: 23px;
      font-size: 24px;
    }
    .checked {
      color: #ff6600;
    }
    .sep-line {
      margin: 0 32px;
    }
    .input {
      width: 348px;
      height: 50px;

      border: 1px solid #e5e5e5;

      input {
        width: 100%;
        height: 100%;
        padding: 18px;
      }
    }

    .btn {
      width: 100%;
      line-height: 50px;
      height: 50px;
      margin-top: 10px;
      font-size: 16px;
    }
    .tips {
      display: flex;
      justify-content: space-between;
      font-size: 14px;
      cursor: pointer;
      .sms {
        color: #ff6600;
      }
      .reg {
        color: #999999;
        span {
          margin: 0 7px;
        }
      }
    }
  }
  .footer {
    height: 100px;
    padding-top: 60px;
    color: #999999;
    font-size: 16px;
    text-align: center;

    .footer-link {
      a {
        color: #999999;
      }
      span {
        margin: 0 10px;
      }
    }
    .copyright {
      margin-top: 13px;
    }
  }
}
</style>
